//
//  ProtolManager.m
//  BossZP
//
//  Created by mosn on 4/28/14.
//  Copyright (c) 2014 com.dlnu.*. All rights reserved.
//

#import "ProtolManager.h"
#import <CoreLocation/CoreLocation.h>

@interface ProtolManager()<CLLocationManagerDelegate>
@property (nonatomic,retain) CLLocationManager *locationManager;

@end


@implementation ProtolManager

+ (ProtolManager *)shareProtolManager
{
    static ProtolManager * singleClient;
    static dispatch_once_t pred;
    dispatch_once(&pred,^{
        singleClient=[[self alloc] initManager];
    } );
    return singleClient;
}


- (id)initManager
{
    if (self=[super init]) {
        [self updateLocation];   ///如果用户允许定位，开始定位
    }
    return self;
}

#pragma mark -
#pragma mark Location
-(void)updateLocation
{
    if(self.locationManager==nil){
        self.locationManager=[[CLLocationManager alloc] init];
#ifdef __IPHONE_8_0
        if (isIOS8) {
            [self.locationManager requestWhenInUseAuthorization];
        }
        
#endif
        self.locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
        self.locationManager.distanceFilter=kCLDistanceFilterNone;
        self.locationManager.delegate=self;
    }
    [self.locationManager stopUpdatingLocation];
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    [self.locationManager stopUpdatingLocation];
    self.locationManager.delegate = nil;
//    [[NSNotificationCenter defaultCenter] postNotificationName:kLocationSuccess object:nil];
}

#pragma mark -
#pragma mark - cllocation delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
    if ([newLocation horizontalAccuracy]>=0 && [newLocation horizontalAccuracy]<1000) {
        [self.locationManager stopUpdatingLocation];
        self.locationManager.delegate = nil;
        self.coor = newLocation.coordinate;
//        [[NSNotificationCenter defaultCenter] postNotificationName:kLocationSuccess object:nil];
    }
   
//    CLGeocoder *coder=[[CLGeocoder alloc] init];
//    [coder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
//        if (error==nil) {
//            self.curentLocation=[placemarks lastObject];
//            [[NSNotificationCenter defaultCenter] postNotificationName:kLocationSuccess object:nil];
//        }
//        
//    }];
}

@end